package com.kly.cashmall.utils.system;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author zxp
 * @since 27/10/21
 */
public class AppListUtil {
    public AppListUtil() {
    }

    public static List<PackageInfo> getAppList(Context context) {
        List<PackageInfo> apps = new ArrayList();
        if (context != null && context.getApplicationContext() != null && context.getApplicationContext().getPackageManager() != null) {
            PackageManager packageManager = context.getApplicationContext().getPackageManager();
            if (packageManager != null) {
                List<PackageInfo> packageList = packageManager.getInstalledPackages(4096);
                if (packageList.size() > 0) {
                    for(int i = 0; i < packageList.size(); ++i) {
                        PackageInfo pak = (PackageInfo)packageList.get(i);
                        if (pak != null && (pak.applicationInfo.flags & 1) <= 0) {
                            apps.add(pak);
                        }
                    }
                }
            }
        }

        return apps;
    }

    private static List<PackageInfo> getPackageList(Context ctx) {
        PackageManager packageManager = ctx.getPackageManager();
        List<PackageInfo> apps = new ArrayList();
        String[] pak = null;

        for(int uid = 1000; uid <= 19999; ++uid) {
            pak = packageManager.getPackagesForUid(uid);
            if (pak != null && pak.length > 0) {
                String[] var5 = pak;
                int var6 = pak.length;

                for(int var7 = 0; var7 < var6; ++var7) {
                    String item = var5[var7];

                    try {
                        PackageInfo packageInfo = packageManager.getPackageInfo(item, 0);
                        if (packageInfo == null) {
                            break;
                        }

                        CharSequence flags = packageManager.getApplicationLabel(packageManager.getApplicationInfo(packageInfo.packageName, 128));
                        Log.d("TAG", "应用名称 = " + flags.toString() + " (" + packageInfo.packageName + ")");
                        if ((packageInfo.applicationInfo.flags & 1) <= 0) {
                            apps.add(packageInfo);
                        }
                    } catch (PackageManager.NameNotFoundException var11) {
                        var11.printStackTrace();
                    }
                }
            }
        }

        return apps;
    }

    public static List<String> getAppListAsShuMeiFormat(Context context) {
        List<PackageInfo> packageInfoList = getAppList(context);
        List<String> stringAppList = new ArrayList();
        if (packageInfoList == null) {
            return stringAppList;
        } else {
            Iterator var3 = packageInfoList.iterator();

            while(var3.hasNext()) {
                PackageInfo packageInfo = (PackageInfo)var3.next();
                String appName = packageInfo.applicationInfo.loadLabel(context.getPackageManager()).toString();
                String packageName = packageInfo.packageName;
                String versionName = packageInfo.versionName;
                long versionCode = (long)packageInfo.versionCode;
                long firstInstallTime = packageInfo.firstInstallTime;
                long lastUpdateTime = packageInfo.lastUpdateTime;
                stringAppList.add(firstInstallTime + "," + packageName + "," + appName + "," + (isSystemPackage(packageInfo) ? 0 : 1) + "," + versionCode + "," + versionName + "," + lastUpdateTime);
            }

            return stringAppList;
        }
    }

    private static boolean isSystemPackage(PackageInfo pkgInfo) {
        return (pkgInfo.applicationInfo.flags & 1) != 0;
    }
}
